unit DMUnit;

interface

uses
  SysUtils, Classes, DB, ADODB,
  IniFiles, encddecd,
  Forms;

type                
  TDM = class(TDataModule)
    procedure DataModuleCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  DM: TDM;

  Database: TADOConnection;

implementation

{$R *.dfm}

procedure TDM.DataModuleCreate(Sender: TObject);
var
  iTry: Integer;
  s: string;
begin
  Database := TADOConnection.Create(Self);
  Database.LoginPrompt := False;

//  Database.ConnectionString := Format(DBFormat, [DBPassword, DBUser, 'BudgetSystem', '192.168.40.39', 9091]);
//  Database.Open;
  with TIniFile.Create(ExtractFilePath(ParamStr(0)) + 'DBConnect.ini') do
  try
    iTry := 0;
    S := Decodestring(ReadString('DBConnect','ConnectionString',''));
    repeat
      try
        Inc( iTry );
        Database.ConnectionString := s;
        Database.Open;
      except
        s := PromptDataSource(0, 'Provider=SQLOLEDB.1;Persist Security Info=True');
      end;
    until( Database.Connected or (iTry >= 3));

    if Database.Connected then
      writeString('DBConnect','ConnectionString',stringreplace(encodestring(S),#13#10,'',[rfReplaceAll]))
    else
      Application.Terminate;
  finally
    Free;
  end;

end;

end.
